package com.supplychainapi.service.impl;

import com.joindata.inf.common.basic.exceptions.ServiceException;
import com.supplychainapi.common.RestResponse;
import com.supplychainapi.common.enums.ExceptionMessage;
import com.supplychainapi.dao.prod.ScmTbdeliveraddrMapper;
import com.supplychainapi.domain.LoginCache;
import com.supplychainapi.model.ScmTbdeliveraddr;
import com.supplychainapi.param.DeliverAddrParam;
import com.supplychainapi.proxy.ValidateHelper;
import com.supplychainapi.service.IScmTbDeliverAddrService;
import com.supplychainapi.utils.JsonUtils;
import com.supplychainapi.utils.StringUtil;
import com.xiaoleilu.hutool.util.BeanUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by panic .
 * Time 2017/11/24
 * description: 地址管理
 */
@Service
public class ScmTbDeliverAddrServiceImpl implements IScmTbDeliverAddrService {
    private Logger logger = LogManager.getLogger(this.getClass());
    @Autowired
    ScmTbdeliveraddrMapper scmTbdeliveraddrMapper;

    @Override
    public RestResponse createAddr(LoginCache loginCache, DeliverAddrParam model) throws ServiceException {
        ScmTbdeliveraddr scmTbdeliveraddr = new ScmTbdeliveraddr();
        BeanUtil.copyProperties(model, scmTbdeliveraddr);

        //新增地址时,将id置为null
        scmTbdeliveraddr.setFiAddrId(Integer.valueOf(autoAddrId(loginCache.getFsShopGUID())));
        scmTbdeliveraddr.setFsCreateTime(new Date());
        scmTbdeliveraddr.setFsShopGUID(loginCache.getFsShopGUID());
        scmTbdeliveraddr.setFsCreateUserId(loginCache.getFsUserName());
        scmTbdeliveraddr.setFsCreateUserName(loginCache.getFsUserName());
        scmTbdeliveraddr.setFsUpdateTime(new Date());
        Integer addressIsExit = scmTbdeliveraddrMapper.
                checkAddressIsExit(model.getFsAddress(), loginCache.getFsShopGUID());
        //地址已经存在
        if (addressIsExit > 0) {
            throw ExceptionMessage.address_isExit.getServiceException();
        }
        //如果是默认地址,将其他地址设为非默认地址
        if (model.getFiDefault() == 1) {
            scmTbdeliveraddrMapper.setUnDefaultAddress(scmTbdeliveraddr.getFsShopGUID());
        }

        int insertSelective = scmTbdeliveraddrMapper.insertSelective(scmTbdeliveraddr);

        if (insertSelective <= 0) {
            throw ExceptionMessage.operation_failed.getServiceException();
        }
        return RestResponse.success();
    }

    @Override
    public RestResponse deleteAddr(String fsShopGUID, Integer addrId) throws ServiceException {
        ScmTbdeliveraddr tbdeliveraddr = new ScmTbdeliveraddr();
        tbdeliveraddr.setFsShopGUID(fsShopGUID);
        tbdeliveraddr.setFiAddrId(addrId);
        if (scmTbdeliveraddrMapper.deleteByPrimaryKey(tbdeliveraddr) < 1) {
            throw ExceptionMessage.address_nofound.getServiceException();
        }
        return RestResponse.success();
    }

    @Override
    public RestResponse updateAddr(LoginCache loginCache, DeliverAddrParam model) throws ServiceException {
        ValidateHelper.validate(model);

        ScmTbdeliveraddr scmTbdeliveraddr = new ScmTbdeliveraddr();
        BeanUtil.copyProperties(model, scmTbdeliveraddr);

        scmTbdeliveraddr.setFsUpdateTime(new Date());
        scmTbdeliveraddr.setFsShopGUID(loginCache.getFsShopGUID());
        scmTbdeliveraddr.setFsUpdateUserId(loginCache.getFsUserId());
        scmTbdeliveraddr.setFsUpdateUserName(loginCache.getFsUserName());
        //如果是默认地址,将其他地址设为非默认地址
        if (model.getFiDefault() == 1) {
            scmTbdeliveraddrMapper.setUnDefaultAddress(loginCache.getFsShopGUID());
        }
        if (scmTbdeliveraddrMapper.updateByPrimaryKeySelective(scmTbdeliveraddr) < 1) {
            throw ExceptionMessage.operation_failed.getServiceException();
        }
        return RestResponse.success();
    }

    @Override
    public RestResponse<List<ScmTbdeliveraddr>> retrieveAllAddr(String shopGuid) throws ServiceException {
        ScmTbdeliveraddr scmTbdeliveraddr = new ScmTbdeliveraddr();
        scmTbdeliveraddr.setFsShopGUID(shopGuid);
        return RestResponse.success(scmTbdeliveraddrMapper.select(scmTbdeliveraddr));
    }

    /**
     * 设置默认地址
     *
     * @param addressId
     * @param shopGuid
     * @return
     * @throws ServiceException
     */
    @Override
    public RestResponse setDefaultAddress(Integer addressId, String shopGuid) throws ServiceException {
        ScmTbdeliveraddr scmTbdeliveraddr = new ScmTbdeliveraddr();
        scmTbdeliveraddr.setFiAddrId(addressId);
        scmTbdeliveraddr.setFsShopGUID(shopGuid);
        //先校验该地址是否存在
        ScmTbdeliveraddr primaryKey = scmTbdeliveraddrMapper.selectByPrimaryKey(scmTbdeliveraddr);
        if (null == primaryKey) {
            throw ExceptionMessage.address_nofound.getServiceException();
        } else {

            BeanUtil.copyProperties(primaryKey, scmTbdeliveraddr);
        }
        scmTbdeliveraddr.setFiDefault((short) 1);
        //先将数据库中所有地址设置为非默认地址
        scmTbdeliveraddrMapper.setUnDefaultAddress(shopGuid);
        //将该地址设置为默认地址
        scmTbdeliveraddrMapper.updateByPrimaryKey(scmTbdeliveraddr);
        return RestResponse.success();
    }

    /**
     * 获取默认地址
     *
     * @param shopGuid
     * @return
     * @throws ServiceException
     */
    @Override
    public RestResponse<List<ScmTbdeliveraddr>> getDefaultAddress(String shopGuid) throws ServiceException {
        ScmTbdeliveraddr scmTbdeliveraddr = new ScmTbdeliveraddr();
        scmTbdeliveraddr.setFsShopGUID(shopGuid);
        scmTbdeliveraddr.setFiDefault((short) 1);
        return RestResponse.success(scmTbdeliveraddrMapper.select(scmTbdeliveraddr));
    }

    @Override
    public String autoAddrId(String shopGuid) {
        String code = scmTbdeliveraddrMapper.getMaxId(shopGuid);
        if (StringUtils.isBlank(code)) {
            return "1001";
        } else {
            return StringUtil.autoGenericCode(code, code.length());
        }
    }
}
